﻿using Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.ViewModels
{
    public class CodeAnalysisConfigurationViewModel : ViewModelBase
    {
        public event EventHandler BrowseRuleSetRequired;

        public event EventHandler Saved;

        public event EventHandler CloseRequired;


        private CodeAnalysisConfiguration configuration;


        public RelayCommand BrowseRuleSetCommand { get; private set; }

        public RelayCommand SaveCommand { get; private set; }

        public RelayCommand CancelCommand { get; private set; }

        public bool EnableCodeAnalysis
        {
            get { return configuration.EnableCodeAnalysis; }
            set { configuration.EnableCodeAnalysis = value; OnPropertyChanged("EnableCodeAnalysis"); }
        }

        public bool OverrideEnableSearchGac
        {
            get { return configuration.OverrideEnableSearchGac; }
            set { configuration.OverrideEnableSearchGac = value; OnPropertyChanged("OverrideEnableSearchGac"); }
        }

        public bool OverrideIgnoreGeneratedCode
        {
            get { return configuration.OverrideIgnoreGeneratedCode; }
            set { configuration.OverrideIgnoreGeneratedCode = value; OnPropertyChanged("OverrideIgnoreGeneratedCode"); }
        }

        public bool OverrideTimeout
        {
            get { return configuration.OverrideTimeout; }
            set { configuration.OverrideTimeout = value; OnPropertyChanged("OverrideTimeout"); }
        }

        public bool OverrideRuleSet
        {
            get { return configuration.OverrideRuleSet; }
            set { configuration.OverrideRuleSet = value; OnPropertyChanged("OverrideRuleSet"); }
        }

        public bool OverrideEnableCodeAnalysisOnBuild
        {
            get { return configuration.OverrideEnableCodeAnalysisOnBuild; }
            set { configuration.OverrideEnableCodeAnalysisOnBuild = value; OnPropertyChanged("OverrideEnableCodeAnalysisOnBuild"); }
        }

        public bool EnableSearchGac
        {
            get { return configuration.EnableSearchGac; }
            set { configuration.EnableSearchGac = value; OnPropertyChanged("EnableSearchGac"); }
        }

        public bool IgnoreGeneratedCode
        {
            get { return configuration.IgnoreGeneratedCode; }
            set { configuration.IgnoreGeneratedCode = value; OnPropertyChanged("IgnoreGeneratedCode"); }
        }

        public double Timeout
        {
            get { return configuration.Timeout; }
            set { configuration.Timeout = value; OnPropertyChanged("Timeout"); }
        }

        public double MinimumTimeout { get { return 0; } }

        public double MaximumTimeout { get { return 180; } }

        public string RuleSet
        {
            get { return configuration.RuleSet; }
            set { configuration.RuleSet = value; OnPropertyChanged("RuleSet"); }
        }

        public bool EnableCodeAnalysisOnBuild
        {
            get { return configuration.EnableCodeAnalysisOnBuild; }
            set { configuration.EnableCodeAnalysisOnBuild = value; OnPropertyChanged("EnableCodeAnalysisOnBuild"); }
        }

        public ICollection<RuleSetViewModel> RuleSets { get; private set; }


        private CodeAnalysisConfigurationViewModel()
        {
            BrowseRuleSetCommand = new RelayCommand(o => OnBrowseRuleSetRequired());
            CancelCommand = new RelayCommand(o => OnCloseRequired());
            RuleSets = new ObservableCollection<RuleSetViewModel>();
        }

        public CodeAnalysisConfigurationViewModel(CodeAnalysisConfiguration configuration)
            : this()
        {
            this.configuration = configuration;
            SaveCommand = new RelayCommand(o => { configuration.Save(); OnSaved(); OnCloseRequired(); });
        }


        protected virtual void OnBrowseRuleSetRequired()
        {
            if (BrowseRuleSetRequired != null)
            {
                BrowseRuleSetRequired(this, EventArgs.Empty);
            }
        }

        protected virtual void OnSaved()
        {
            if (Saved != null)
            {
                Saved(this, EventArgs.Empty);
            }
        }

        protected virtual void OnCloseRequired()
        {
            if (CloseRequired != null)
            {
                CloseRequired(this, EventArgs.Empty);
            }
        }
    }
}
